Skip to content

HTTP高级请求 - HttpRequestEx

函数简介

高级HTTP请求,支持自定义Method、请求头(含Cookie)、请求体。

接口名称

HttpRequestEx

DLL调用

c
const char* HttpRequestEx(int64_t instance, const char* method, const char* url, const char* headers, const char* body, const char* content_type, int32_t* status_code);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
method字符串方法,如"GET"/"POST"/"PUT"/"DELETE",大小写不敏感
url字符串完整URL
headers字符串自定义请求头,多行字符串,每行"Name: Value",可为空
body字符串请求体,GET可传空
content_type字符串如"application/json",可为空
status_code整数型指针输出HTTP状态码,可为NULL

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
int status = 0;
string resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/status", "", "", "", status);
// status 为 HTTP 状态码
csharp
using OLAPlug;

var ola = new OLAPlugServer();
int status = 0;
string resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/status", "", "", "", status);
// status 为 HTTP 状态码
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
status = 0
resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/status", "", "", "", status)
# status 为 HTTP 状态码
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
int status = 0;
String resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/status", "", "", "", status);
// status 为 HTTP 状态码
cpp
var ola = com("OlaPlug.OlaSoft")
var status = 0
var resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/status", "", "", "", status)
// status 为 HTTP 状态码
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
status = 0
resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/status", "", "", "", status)
' status 为 HTTP 状态码
text
.局部变量 ola, OLAPlug
ola.创建 ()
status = 0
resp = ola.HttpRequestEx(“GET“, “https://api.example.com/v1/status“, ““, ““, ““, status)
' status 为 HTTP 状态码
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var status = 0;
var resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/status", "", "", "", status);
// status 为 HTTP 状态码
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
整数 status = 0
文本型 resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/status", "", "", "", status)
// status 为 HTTP 状态码
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
int status = 0;
string resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/status", "", "", "", status);
// status 为 HTTP 状态码

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
int status = 0;
long respPtr = HttpRequestEx(instance, "GET", "https://api.example.com/v1/status", "", "", "", status);
if (respPtr != 0) {
    char resp[512] = {0};
    GetStringFromPtr(respPtr, resp, sizeof(resp));
    FreeStringPtr(respPtr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();

long instance = CreateCOLAPlugInterFace();
int status = 0;
long respPtr = HttpRequestEx(instance, "GET", "https://api.example.com/v1/status", "", "", "", status);
if (respPtr != 0) {
    StringBuilder resp = new StringBuilder(GetStringSize(respPtr) + 1);
    GetStringFromPtr(respPtr, resp, resp.Capacity);
    FreeStringPtr(respPtr);
    string respStr = resp.ToString();
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
status = 0
respPtr = HttpRequestEx(instance, "GET", "https://api.example.com/v1/status", "", "", "", status)
if respPtr:
    buf = create_string_buffer(512)
    ola.GetStringFromPtr(respPtr, buf, 512)
    ola.FreeStringPtr(respPtr)
    resp = buf.value.decode("utf-8")

返回值

字符串指针,返回响应体内容,失败返回NULL。需调用 FreeStringPtr 释放内存。

注意事项

  • 返回的字符串需要调用 FreeStringPtr 释放内存
  • headers格式:每行"Name: Value",以"\r\n"分隔
  • 支持的HTTP方法:GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS等
  • status_code可以为NULL,如果不需要获取状态码